package com.sky.service.impl;

import com.sky.domain.Blog;
import com.sky.mapper.BlogMapper;
import com.sky.service.BlogService;
import com.sky.utils.MarkdownUtils;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * @author 尹稳健~
 * @version 1.0
 */
@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogMapper blogMapper;

    @Override
    public List<Blog> getAllBlog() {
        return blogMapper.getAllBlog();
    }

    @Override
    public void saveBlog(Blog blog) {
        blogMapper.saveBlog(blog);
    }

    @Override
    public Blog getBlogById(Long id) {
        Blog blog = blogMapper.getBlogById(id);
        String content = blog.getContent();
        blog.setContent(MarkdownUtils.markdownToHtmlExtensions(content));  //将Markdown格式转换成html
        return blog;
    }

    @Override
    public void updateBlog(Blog blog) {
        blogMapper.updateBlog(blog);
    }

    @Override
    public void deleteBlog(Long id) {
        blogMapper.deleteBlog(id);
    }

    @Override
    public List<Blog> searchAllBlog(Blog blog) {
        return blogMapper.searchAllBlog(blog);
    }

    @Override
    public List<Blog> getByTagId(Long id) {
        return blogMapper.getByTagId(id);
    }

    @Override
    public List<Blog> getByTypeId(Long id) {
        return blogMapper.getByTypeId(id);
    }

    @Override
    public Map<String, List<Blog>> archiveBlog() {
        List<String> groupYear = blogMapper.findGroupYear();
        HashSet<String> set = new HashSet<>(groupYear);
        HashMap<String, List<Blog>> map = new HashMap<>();
        for (String year : set) {
            map.put(year,blogMapper.findByYear(year));
        }
        return map;
    }

    @Override
    public List<Blog> getAllRecommendBlog() {
        return blogMapper.getAllRecommendBlog();
    }

    @Override
    public List<Blog> getSearchBlog(String query) {
        return blogMapper.getSearchBlog(query);
    }
}
